<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page contentType="text/html; charset=UTF-8" import="demo.*"%> 
<%@ page import="java.sql.*"%> 

<HEAD><TITLE>Registration</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">

</HEAD>

<BODY leftMargin=0 topMargin=10>

<TABLE cellSpacing=0 cellPadding=0 width=700 align=center border=0>
  <TBODY>
  <TR>
    <TD>
      <DIV align=center><A href="index.jsp"><IMG height=118 
      src="Pics/restaurantLogo.JPG" width=242 
  border=0></A></DIV></TD></TR></TBODY></TABLE>

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0><TR>
    <TD vAlign=top height=60>   
      
              <TABLE cellSpacing=1 cellPadding=3 width="60%" align=center 
      bgColor=#cccccc border=0>
        <TBODY>
	
		<tr bgColor=#bbbbbb><td align = "center" colspan = 7>Order Form</td></tr>
		<tr><td>Form Number</td><td>Order Time</td><td>Order Status</td><td>Price</td></tr>
		<% 
			DBConnector db = DBConnector.getDBConnector();
			String CusID = "1"; //Assume the customer is always the same one
			
			String AllOrderForms = "select form_ID from order_form where cus_ID = " + CusID + " order by form_time desc";
			db.prepareStatement(AllOrderForms);
			ResultSet AllOrderFormsResult = db.queryPstmt();
			if (AllOrderFormsResult != null)
			{
				//for all the order forms of the current customer
				while (AllOrderFormsResult.next())
				{
					String FormID = AllOrderFormsResult.getString(1);
					String PriceOfOrderForm = "select food_price, order_quantity from food, contain, order_form" +
					" where contain.food_ID = food.food_ID and contain.form_ID = order_form.form_ID and " +
					"order_form.form_ID = " + FormID;
					db.prepareStatement(PriceOfOrderForm);
					ResultSet PriceOfOrderFormResult = db.queryPstmt();
					if (PriceOfOrderFormResult != null)
					{				
						String QueryOrderForms = "select form_ID, form_time, form_state from order_form where form_ID = " + FormID;
						db.prepareStatement(QueryOrderForms);
						ResultSet QueryOrderFormsResult = db.queryPstmt();
						
						out.println("<FORM name='order_form' action='customer_account.jsp' method=post>");
						out.println("<tr>");
						if (QueryOrderFormsResult != null)
						{
							if (QueryOrderFormsResult.next())
							{
								out.println("<td>" + QueryOrderFormsResult.getString(1) + "</td>");
								out.println("<td>" + QueryOrderFormsResult.getString(2) + "</td>");
								out.println("<td>" + QueryOrderFormsResult.getString(3) + "</td>");								
							}
							
							float TotalPrice = 0;
							while (PriceOfOrderFormResult.next())
							{
								TotalPrice += PriceOfOrderFormResult.getFloat(1) * PriceOfOrderFormResult.getInt(2);
							}						
							String TotalPriceString = Float.toString(TotalPrice);
							out.println("<td>" + TotalPriceString + "</td>");
							
							String orderStatus = QueryOrderFormsResult.getString(3);
							out.println("<td><input name = 'CheckOrderForm' type = 'radio' class = 'input' value = '" + QueryOrderFormsResult.getString(1) + "'>Check</td>");
							if (orderStatus.equals("not cooked yet") || orderStatus.equals("cooking"))
							{
								out.println("<td><input name = 'CancelOrderForm' type = 'radio' class = 'input' value = '" + QueryOrderFormsResult.getString(1) + "'>Cancel</td>");
							}	
							else
								out.println("<td></td>");			
							out.println("<TD><INPUT type=submit value=apply name=SubmitSearchOrderForm></TD>");			
							out.println("</tr>");
							out.println("</FORM>");
						}						
					}					
				}				
			}
		%>        
          </TBODY></TABLE>		
          
          <TABLE cellSpacing=1 cellPadding=3 width="60%" align=center 
      bgColor=#eeeeee border=0>
      
      <%
      	String CheckOrderForm = request.getParameter("CheckOrderForm");
      	if (CheckOrderForm != null && CheckOrderForm.length() > 0)
      	{      		
      		String QueryOrderForm = "select receiver_name, receiver_add, receiver_phone, form_time, food_name, food_price, order_quantity  from order_form, contain, food " +
      		"where order_form.form_ID = contain.form_ID and contain.food_ID = food.food_ID and order_form.form_ID = " + CheckOrderForm;
      		db.prepareStatement(QueryOrderForm);
      		ResultSet QueryOrderFormResult = db.queryPstmt();
      		
      		if (QueryOrderFormResult != null)
      		{	
      			QueryOrderFormResult.next();
      			String receiver_name = QueryOrderFormResult.getString(1);
      			String receiver_add = QueryOrderFormResult.getString(2);
      			String receiver_phone = QueryOrderFormResult.getString(3);
      			
      			String QueryCustomerInfo = "select cus_name, cus_address, cus_phone from customer, order_form where order_form.cus_ID = customer.cus_ID and form_ID = " + CheckOrderForm;
      			db.prepareStatement(QueryCustomerInfo);
      			ResultSet QueryCustomerInfoResult = db.queryPstmt();
      			if (QueryCustomerInfoResult != null && QueryCustomerInfoResult.next())
      			{
      				if (receiver_name == null)
      					receiver_name = QueryCustomerInfoResult.getString(1);
      				if (receiver_add == null)
      					receiver_add = QueryCustomerInfoResult.getString(2);
      				if (receiver_phone == null)
      					receiver_phone = QueryCustomerInfoResult.getString(3);
      			}
      			out.println("<tr><td aligh = right>Receiver Name : </td><td>" + receiver_name + "</td></tr>");
      			out.println("<tr><td aligh = right>Receiver Address: </td><td>" + receiver_add + "</td></tr>");
      			out.println("<tr><td aligh = right>Receiver Phone : </td><td>" + receiver_phone + "</td></tr>");
      			out.println("<tr><td aligh = right>Order Number : </td><td>" + CheckOrderForm + "</td></tr>");
      			out.println("<tr><td aligh = right>Order Time : </td><td>" + QueryOrderFormResult.getString(4) + "</td></tr>");
      			out.println("<tr><td><br></td><tr>");
      			
      			out.println("<tr><td aligh = right>Food</td><td aligh = right>Price</td><td aligh = right>Quantity</td></tr>");
      			float TotalPrice = 0;
      			do{
      				TotalPrice += QueryOrderFormResult.getFloat(6) * QueryOrderFormResult.getInt(7);      				
      				out.println("<tr><td aligh = right>" + QueryOrderFormResult.getString(5) 
      							+ "</td><td aligh = right>" + QueryOrderFormResult.getString(6) 
      							+ "</td><td aligh = right>" + QueryOrderFormResult.getString(7) + "</td></tr>");
      			}while (QueryOrderFormResult.next()); 
      			out.println("<tr><td>Total Price : </td><td>" + Float.toString(TotalPrice) + "</td></tr>");			
      			
      		}
      		
      	}
      	
      	String CancelOrderForm = request.getParameter("CancelOrderForm");
      	
      	if (CancelOrderForm != null && CancelOrderForm.length() > 0)
      	{
	      	String CancelOrderFormStatement = "delete from order_form where form_ID = " + CancelOrderForm;
	      	out.println(CancelOrderFormStatement);
	      	db.prepareStatement(CancelOrderFormStatement);
	      	db.updatePstmt();
	      	String resultURL = "<script type =\"text/javascript\"> window.location.href = 'customer_account.jsp'</script>";
	      	out.println(resultURL);
      	}
       %>      
      </TABLE>
         

    </TD>
      </TR>    
      
      </TABLE>  
</BODY>
